#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

/** Our old friend die from ex17. */
void die(const char *message)
{
    if(errno) {
        perror(message);
    } else {
        printf("ERROR: %s\n", message);
    }

    exit(1);
}

// a typedef creates a fake type, in this
// case for a function pointer
typedef int (*compare_cb)(int a, int b);
//函数指针，表明了一个类型为compare_cb的函数，并定义这种类型为指向某种函数的指针，接收int为参数返回int


/**
 * A classic bubble sort function that uses the
 * compare_cb to do the sorting.
 */
int *bubble_sort(int *numbers, int count, compare_cb cmp)
{
    int temp = 0;
    int i = 0;
    int j = 0;
    int *target = malloc(count * sizeof(int));

    if(!target) die("Memory error.");

    memcpy(target, numbers, count * sizeof(int));

    for(i = 0; i < count; i++) {
        for(j = 0; j < count - 1; j++) {
            if(cmp(target[j], target[j+1]) > 0) {
                temp = target[j+1];
                target[j+1] = target[j];
                target[j] = temp;
            }
        }
    }

    return target;
}


int *select_sort(int *numbers, int count, compare_cb cmp)
{
    int temp = 0;
    int i = 0;
    int j = 0;
    int min;
    int *target = malloc(count * sizeof(int));

    if(!target) die("Memory error.");

    memcpy(target, numbers, count * sizeof(int));

    for(i = 0; i < count; i++) {
        min = i;
        for(j = i+1 ; j < count - 1; j++) {
            if(cmp(target[j], target[min]) < 0) {
                min = j;
            }
        }
        temp = target[min];
        target[min] = target[i];
        target[i] = temp;
    }

    return target;
}


int bubble_sorted_order(int a, int b)
{
    return a - b;
}

int reverse_order(int a, int b)
{
    return b - a;
}

int strange_order(int a, int b)
{
    if(a == 0 || b == 0) {
        return 0;
    } else {
        return a % b;
    }
}

/**
 * Used to test that we are sorting things correctly
 * by doing the sort and printing it out.
 */
void test_sorting(int *numbers, int count, compare_cb cmp)
{
    int i = 0;
    int *bubble_sorted = bubble_sort(numbers, count, cmp);
    int *select_sorted = select_sort(numbers, count, cmp);
    if(!bubble_sorted) die("Failed to sort as requested.");
    if(!select_sorted) die("Failed to sort as requested.");
    for(i = 0; i < count; i++) {
        printf("%d ", bubble_sorted[i]);
    }
    printf("\n");

    for(i = 0; i < count; i++) {
        printf("%d ", select_sorted[i]);
    }
    printf("\n");
    free(bubble_sorted);
    free(select_sorted);
// add************************************ 
    // unsigned char *data = (unsigned char *)cmp;

    // for(i = 0; i < 25; i++) {
    //     printf("%02x:", data[i]);
    // }
    // printf("\n");
}


int main(int argc, char *argv[])
{
    if(argc < 2) die("USAGE: ex18 4 3 1 5 6");

    int count = argc - 1;
    int i = 0;
    char **inputs = argv + 1;

    int *numbers = malloc(count * sizeof(int));
    if(!numbers) die("Memory error.");

    for(i = 0; i < count; i++) {
        numbers[i] = atoi(inputs[i]);
    }

    test_sorting(numbers, count, bubble_sorted_order);
    test_sorting(numbers, count, reverse_order);
    test_sorting(numbers, count, strange_order);



    free(numbers);

    return 0;
}